package com.pirestupppai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.widgets.view.AvatarView;

import java.util.List;

/**
 * author: xiaoluo
 * date: 2017/5/23 14:15
 */
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyViewHolder>{
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    private Context mContext;
    private List<FeedItem> mList;
    private LayoutInflater mInflater;

    private View mFooterView;

    public VideoListAdapter(Context context, List<FeedItem> list) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (mFooterView == null) {
            return TYPE_ITEM;
        }
        if (position == mList.size()) {
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new MyViewHolder(mFooterView);
        }
        View view = mInflater.inflate(R.layout.item_ps_list, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_FOOTER) {
            return;
        }

//        final FeedItem item = mList.get(position);
//        holder.psTime.setText(StringUtil.transTime(item.getCreate_time()));
//        holder.psDesc.setText(item.getDesc());
//        holder.psNickname.setText(item.getNickname());
//        ImageLoadUtil.getInstance().loadAvatar(mContext, item.getAvatar(), holder.psAvatar);
//        holder.psAvatar.setUid(item.getUid(), item.is_star());
//        ImageLoadUtil.getInstance().loadImage(mContext, item.getImage_url(),
//                holder.psImage, ImageLoadUtil.LoadOption.LOAD_MEDIUM);
//
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, VideoTuttiActivity.class);
//                mContext.startActivity(intent);
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return mFooterView == null ? mList.size() : mList.size() + 1;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView psImage;
        AvatarView psAvatar;
        TextView psNickname;
        TextView psDesc;
        TextView psTime;
        ImageView videoImage;

        public MyViewHolder(View itemView) {
            super(itemView);

            if (itemView == mFooterView) {
                return;
            }

            psImage = (ImageView) itemView.findViewById(R.id.ps_image);
            psAvatar = (AvatarView) itemView.findViewById(R.id.ps_avatar);
            psNickname = (TextView) itemView.findViewById(R.id.ps_nickname);
            psDesc = (TextView) itemView.findViewById(R.id.ps_desc);
            psTime = (TextView) itemView.findViewById(R.id.ps_time);
            videoImage = (ImageView) itemView.findViewById(R.id.video_image);
            videoImage.setVisibility(View.VISIBLE);
        }
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
    }
}